package com.explorati.LeetCode404.sumOfLeftLeaves;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 16:07 2020/9/19 0019
 * @ Description ：
 */
public class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    /**
     * 计算给定二叉树的所有左叶子之和。
     * @param root
     * @return
     */
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int sum = 0;

        if(root.left != null && root.left.left == null && root.left.right == null) {
            sum = root.left.val;
        }

        return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }
}
